package com.sxt.springsecuritytest2.service.impl;

import com.sxt.springsecuritytest2.bean.SysRole;
import com.sxt.springsecuritytest2.bean.SysUser;
import com.sxt.springsecuritytest2.mapper.SysRoleMapper;
import com.sxt.springsecuritytest2.mapper.SysUserMapper;
import com.sxt.springsecuritytest2.service.SysUserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/*
 *
 * @Description todo
 * @author qqg
 * @date $date$
 * @param $params$ 
 * @return $return$
 **/
@Service
public class SysUserServiceImpl implements SysUserService{

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return sysUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SysUser record) {
        return sysUserMapper.insert(record);
    }

    @Override
    public int insertSelective(SysUser record) {
        return sysUserMapper.insertSelective(record);
    }

    @Override
    public SysUser selectByPrimaryKey(Integer id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SysUser record) {
        return sysUserMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SysUser record) {
        return sysUserMapper.updateByPrimaryKey(record);
    }

    //根据用户名查找用户。
     @Override
     public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
         UserDetails result = sysUserMapper.selectByUserName(s);
         //查找用户信息
         List<SysRole> byUid = sysRoleMapper.findByUid(((SysUser) result).getId());
//         List<String> roleName = new ArrayList<>();
         //将用户的角色信息查找出来，放到集合里
         ((SysUser) result).setRoles(byUid);
         System.out.println(result.toString());
         System.out.println(byUid.toString());
         return result;
     }
 }
